home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gdevmsxf.c < prev    next >
C/C++ Source or Header  |  1993-05-27  |  14KB  |  438 lines

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevmsxf.c */
  20. /* External font (xfont) implementation for Microsoft Windows. */
  21. #include "ctype_.h"
  22. #include "math_.h"
  23. #include "memory_.h"
  24. #include "string_.h"
  25. #include "gdevmswn.h"
  26. #include "gsutil.h"
  27. #include "gxxfont.h"
  28.  
  29. /* Imported from gdevwdib.c or gdevwddb.c */
  30. extern gx_device gs_mswin_device;
  31.  
  32. /* Imported from gdevemap.c */
  33. extern const byte far_data gs_map_std_to_iso[256];
  34.  
  35. /* Declare the xfont procedures */
  36. private xfont_proc_lookup_font(win_lookup_font);
  37. private xfont_proc_char_xglyph(win_char_xglyph);
  38. private xfont_proc_char_metrics(win_char_metrics);
  39. private xfont_proc_render_char(win_render_char);
  40. private xfont_proc_release(win_release);
  41. private gx_xfont_procs win_xfont_procs = {
  42.     win_lookup_font,
  43.     win_char_xglyph,
  44.     win_char_metrics,
  45.     win_render_char,
  46.     win_release
  47. };
  48.  
  49. /* Return the xfont procedure record. */
  50. gx_xfont_procs *
  51. win_get_xfont_procs(gx_device *dev)
  52. {    return &win_xfont_procs;
  53. }
  54.  
  55. /* Define a Windows xfont. */
  56. typedef struct win_xfont_s win_xfont;
  57. struct win_xfont_s {
  58.     gx_xfont_common common;
  59.     LOGFONT lf;
  60.     TEXTMETRIC tm;
  61.     HFONT hFont;
  62.     gx_device_win *dev;                /* for GetDC */
  63.     int invert_y;
  64.     int y_offset;
  65. };
  66. #define wxf ((win_xfont *)xf)
  67.  
  68. /* Forward references */
  69. private HDC near win_get_dc(P1(gx_device_win *));
  70. private void near win_release_dc(P2(gx_device_win *, HDC));
  71. private int win_select_font(P2(HDC, win_xfont *));
  72.  
  73. /* Map from PostScript to Windows character codes. */
  74. /* (These tables were generated by winmaps.ps.) */
  75.  
  76. private const byte far_data gs_map_symbol_to_oem[256] = {
  77.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  78.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  79.   32, 33, 0, 35, 0, 37, 38, 0, 40, 41, 0, 43, 44, 0, 46, 47,
  80.   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  81.   0, 0, 0, 0, 0, 0, 231, 225, 0, 0, 0, 0, 0, 0, 0, 0,
  82.   226, 232, 0, 227, 0, 0, 0, 233, 0, 0, 0, 91, 0, 93, 0, 95,
  83.   0, 223, 224, 0, 234, 0, 236, 0, 0, 0, 0, 0, 0, 229, 0, 0,
  84.   0, 0, 0, 228, 230, 0, 0, 0, 0, 0, 0, 123, 124, 125, 0, 0,
  85.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  86.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  87.   0, 0, 0, 242, 0, 235, 0, 5, 4, 3, 6, 29, 27, 24, 26, 25,
  88.   247, 240, 0, 241, 0, 0, 0, 7, 245, 0, 239, 0, 0, 0, 0, 0,
  89.   0, 0, 0, 0, 0, 0, 0, 238, 0, 0, 0, 0, 0, 0, 237, 0,
  90.   0, 0, 0, 0, 0, 0, 250, 248, 169, 0, 0, 0, 0, 0, 0, 0,
  91.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  92.   0, 0, 0, 243, 0, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  93. };
  94.  
  95. private const byte far_data gs_map_iso_to_oem[256] = {
  96.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  97.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  98.   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 0, 46, 47,
  99.   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  100.   64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  101.   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  102.   96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  103.   112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 0,
  104.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  105.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  106.   32, 172, 154, 155, 0, 156, 0, 21, 0, 0, 165, 173, 169, 45, 0, 0,
  107.   247, 240, 252, 0, 0, 229, 20, 249, 0, 0, 166, 174, 171, 170, 0, 167,
  108.   0, 0, 0, 0, 141, 142, 145, 128, 0, 143, 0, 0, 0, 0, 0, 0,
  109.   0, 164, 0, 0, 0, 0, 152, 0, 0, 0, 0, 0, 153, 0, 0, 0,
  110.   133, 159, 131, 0, 132, 134, 144, 135, 138, 130, 136, 137, 140, 160, 0, 139,
  111.   0, 163, 148, 161, 146, 0, 147, 245, 0, 150, 162, 149, 129, 0, 0, 151
  112. };
  113.  
  114. /* Correlate PostScript font names with Windows font names. */
  115. /* This table should be an external resource, like Fontmap, */
  116. /* but that will have to wait till later. */
  117.  
  118. typedef struct font_entry_s {
  119.     const char _ds *key;
  120.     const char _ds *value;
  121.     uint pitchAndFamily;
  122. } font_entry;
  123.  
  124. private const font_entry font_names[] = {
  125.     {"Courier", "Courier New", FIXED_PITCH | FF_MODERN},
  126.     {"Helvetica", "Arial", VARIABLE_PITCH | FF_SWISS},
  127.     {"Helvetica", "Helv", VARIABLE_PITCH | FF_SWISS},
  128.     {"Times", "Times New Roman", VARIABLE_PITCH | FF_ROMAN},
  129.     {"Times", "Tms Rmn", VARIABLE_PITCH | FF_ROMAN}
  130. };
  131.  
  132. /* Look up a font. */
  133. private int /*bool*/ map_logical_font(P2(HDC, win_xfont *));
  134. gx_xfont *
  135. win_lookup_font(gx_device *dev, const byte *fname, uint len,
  136.   int encoding_index, const gs_uid *puid, const gs_matrix *pmat,
  137.   const gs_memory_procs *mprocs)
  138. {    win_xfont f;
  139.     win_xfont *wf;
  140.     uint name_len = min(len, LF_FACESIZE-1);
  141.     const font_entry _ds *pfe;
  142.     HDC hdc;
  143.     /* Only handle simple cases for now. */
  144.     if ( pmat->xy != 0 || pmat->yx != 0 || pmat->xx <= 0 ||
  145.          fabs(fabs(pmat->yy) - pmat->xx) > 0.00002
  146.        )
  147.         return NULL;
  148.     f.lf.lfHeight = pmat->xx * 1000;
  149.     if ( f.lf.lfHeight <= 0 || f.lf.lfHeight >= 36 )
  150.         return NULL;
  151.     f.lf.lfWidth = 0;
  152.     f.lf.lfEscapement = 0;
  153.     f.lf.lfOrientation = 0;
  154.     f.lf.lfWeight =
  155.         (string_match(fname, len, "*Bold*", 6, NULL) ?
  156.          FW_BOLD : FW_REGULAR);
  157.     f.lf.lfItalic =
  158.         string_match(fname, len, "*Italic*", 8, NULL) ||
  159.         string_match(fname, len, "*Oblique*", 9, NULL);
  160.     f.lf.lfUnderline = 0;
  161.     f.lf.lfStrikeOut = 0;
  162.     f.lf.lfCharSet =
  163.         (encoding_index == 2 ? SYMBOL_CHARSET : ANSI_CHARSET);
  164.     f.lf.lfOutPrecision = OUT_CHARACTER_PRECIS;
  165.     f.lf.lfClipPrecision = CLIP_STROKE_PRECIS;
  166.     f.lf.lfQuality = PROOF_QUALITY;
  167.     f.hFont = 0;
  168.     f.invert_y = pmat->yy >= 0;
  169.     hdc = win_get_dc(wdev);
  170.     if ( hdc == NULL )
  171.         return NULL;
  172.     for ( pfe = font_names; pfe != &font_names[countof(font_names)]; pfe++ )
  173.       if ( !strncmp(pfe->key, fname, strlen(pfe->key)) )
  174.     {    /* Found a match. */
  175.         strcpy(f.lf.lfFaceName, pfe->value);
  176.         f.lf.lfPitchAndFamily = pfe->pitchAndFamily;
  177.         if ( map_logical_font(hdc, &f) )
  178.             break;
  179.     }
  180.     if ( f.hFont == 0 )
  181.     {    /* No matches in the table, try with the given name. */
  182.         uint len;
  183.         memcpy(f.lf.lfFaceName, fname, name_len);    /* default */
  184.         for ( len = 0; len < name_len; len++ )
  185.           if ( !isalnum(fname[len]) )
  186.             break;
  187.         f.lf.lfFaceName[len] = 0;
  188.         f.lf.lfPitchAndFamily = 0;    /* default */
  189.         if ( !map_logical_font(hdc, &f) )
  190.         {    win_release_dc(wdev, hdc);
  191.             return NULL;
  192.         }
  193.     }
  194.     GetTextMetrics(hdc, &f.tm);
  195.     win_release_dc(wdev, hdc);
  196.     f.y_offset = (!f.invert_y ? f.tm.tmAscent : f.tm.tmDescent);
  197.     wf = (win_xfont *)(*mprocs->alloc)(1, sizeof(win_xfont), "win_lookup_font");
  198.     if ( wf == 0 )
  199.     {    DeleteObject(f.hFont);
  200.         return NULL;
  201.     }
  202.     f.common.procs = &win_xfont_procs;
  203.     f.dev = wdev;
  204.     *wf = f;
  205.     return (gx_xfont *)wf;
  206. }
  207. /* Map the logical font, and see if the result is satisfactory. */
  208. private int /*bool*/
  209. map_logical_font(HDC hdc, win_xfont *xf)
  210. {    char szFaceName[LF_FACESIZE];
  211.     xf->hFont = CreateFontIndirect(&xf->lf);
  212.     if ( xf->hFont == 0 )
  213.         return 0;
  214.     /* Check the face name */
  215.     SelectObject(hdc, xf->hFont);
  216.     GetTextFace(hdc, sizeof(szFaceName), szFaceName);
  217.     if ( !strncmp(xf->lf.lfFaceName, szFaceName, strlen(xf->lf.lfFaceName)) )
  218.         return 1;
  219.     DeleteObject(xf->hFont);
  220.     xf->hFont = 0;
  221.     return 0;
  222. }
  223.  
  224. /* Convert a character name or index to an xglyph code. */
  225. gx_xglyph
  226. win_char_xglyph(gx_xfont *xf, gs_char chr, int encoding_index,
  227.   gs_glyph glyph, gs_proc_glyph_name_t glyph_name_proc)
  228. {    if ( chr == gs_no_char )
  229.         return gx_no_xglyph;    /* can't look up names yet */
  230.     if ( encoding_index == 0 )
  231.     {    /* Map StandardEncoding to ISOLatin1Encoding. */
  232.         /* We lose a couple of characters that exist in both */
  233.         /* StandardEncoding and the Windows OEM encoding but not in */
  234.         /* the ISOLatin1Encoding; we won't worry about this */
  235.         /* for now. */
  236.         chr = gs_map_std_to_iso[chr];
  237.         encoding_index = 1;
  238.     }
  239.     if ( wxf->hFont == NULL )
  240.     {    /* TEXTMETRICS not filled in yet */
  241.         HDC hdc = win_get_dc(wxf->dev);
  242.         int code;
  243.         if ( hdc == NULL )
  244.             return gx_no_xglyph;
  245.         code = win_select_font(hdc, wxf);
  246.         win_release_dc(wxf->dev, hdc);
  247.         if ( code < 0 )
  248.             return gx_no_xglyph;
  249.     }
  250.     switch ( wxf->tm.tmCharSet )
  251.     {
  252.     case ANSI_CHARSET:
  253.         if ( encoding_index == 1 && (chr < 0x7f || chr > 0x9f ||
  254.              chr == 0x91 || chr == 0x92)
  255.            )
  256.             break;
  257.         return gx_no_xglyph;
  258.     case OEM_CHARSET:
  259.         switch ( encoding_index )
  260.         {
  261.         case 1:                /* ISOLatin1 */
  262.             chr = gs_map_iso_to_oem[chr];
  263.             break;
  264.         case 2:                /* Symbol */
  265.             chr = gs_map_symbol_to_oem[chr];
  266.             break;
  267.         default:
  268.             return gx_no_xglyph;
  269.         }
  270.         break;
  271.     default:
  272.         return gx_no_xglyph;
  273.     }
  274.     return (chr != 0 && chr >= wxf->tm.tmFirstChar &&
  275.         chr <= wxf->tm.tmLastChar ?
  276.         (gx_xglyph)chr : gx_no_xglyph);
  277. }
  278.  
  279. /* Get the metrics for a character. */
  280. int
  281. win_char_metrics(gx_xfont *xf, gx_xglyph xg, int wmode,
  282.   gs_int_point *pwidth, gs_int_rect *pbbox)
  283. {    int code;
  284.     DWORD extent;
  285.     HDC hdc;
  286.     char chr = (char)xg;
  287.     if ( wmode != 0 )
  288.         return gs_error_undefined;
  289.     hdc = win_get_dc(wxf->dev);
  290.     if ( hdc == NULL )
  291.         return gs_error_limitcheck;
  292.     if ( (code = win_select_font(hdc, wxf)) < 0 )
  293.     {    win_release_dc(wxf->dev, hdc);
  294.         return code;
  295.     }
  296.     extent = GetTextExtent(hdc, &chr, 1);
  297.     win_release_dc(wxf->dev, hdc);
  298.     pwidth->x = LOWORD(extent);
  299.     pwidth->y = 0;
  300.     pbbox->p.x = 0;
  301.     pbbox->q.x = LOWORD(extent);
  302.     if ( wxf->invert_y )
  303.     {    pbbox->p.y = -wxf->tm.tmDescent;
  304.         pbbox->q.y = wxf->tm.tmAscent;
  305.     }
  306.     else
  307.     {    pbbox->p.y = -wxf->tm.tmAscent;
  308.         pbbox->q.y = wxf->tm.tmDescent;
  309.     }
  310.     return 0;
  311. }
  312.  
  313. /* Render a character. */
  314. int
  315. win_render_char(gx_xfont *xf, gx_xglyph xg, gx_device *dev,
  316.   int xo, int yo, gx_color_index color, int required)
  317. {    char chr = (char)xg;
  318.     int code;
  319.     if ( dev->dname == gs_mswin_device.dname &&
  320.          wdev->hdctext != NULL && !wxf->invert_y
  321.        )
  322.     {    /* Display the character directly */
  323.         HDC hdc = wdev->hdctext;
  324.         PALETTEENTRY *pal = &wdev->limgpalette->palPalEntry[color];
  325.         if ( (code = win_select_font(hdc, wxf)) < 0 )
  326.             return code;
  327.         SetTextColor(hdc, RGB(pal->peRed, pal->peGreen, pal->peBlue));
  328.         SetBkMode(hdc, TRANSPARENT);
  329.         TextOut(hdc, xo, yo - wxf->y_offset, &chr, 1);
  330.     }
  331.     else if ( !required )
  332.         code = -1;        /* too hard */
  333.     else
  334.     {    /* Display on an intermediate bitmap, then copy the bits. */
  335.         gs_int_point wxy;
  336.         gs_int_rect bbox;
  337.         int w, h, wbm, raster;
  338.         gx_device_win *fdev = wxf->dev;
  339.         HBITMAP hbm;
  340.         byte *bits;
  341.         code = (*xf->common.procs->char_metrics)(xf, xg, 0,
  342.                              &wxy, &bbox);
  343.         if ( code < 0 )
  344.             return code;
  345.         w = bbox.q.x - bbox.p.x;
  346.         h = bbox.q.y - bbox.p.y;
  347.         wbm = round_up(w, align_bitmap_mod * 8);
  348.         raster = wbm >> 3;
  349.         bits = gs_malloc(h, raster, "win_render_char");
  350.         if ( bits == 0 )
  351.             return gs_error_limitcheck;
  352.         hbm = CreateBitmap(wbm, h, 1, 1, NULL);
  353.         if ( hbm == NULL )
  354.         {    code = gs_error_limitcheck;
  355.         }
  356.         else
  357.         {    HDC hdcwin = win_get_dc(fdev);
  358.             HDC hdcbit = CreateCompatibleDC(hdcwin);
  359.             dev_proc_copy_mono((*copy_mono)) =
  360.                 dev->procs->copy_mono;
  361.             int y = yo - wxf->y_offset;
  362.             SetMapMode(hdcbit, GetMapMode(hdcwin));
  363.             win_select_font(hdcbit, wxf);
  364.             SelectObject(hdcbit, hbm);
  365.             PatBlt(hdcbit, 0, 0, wbm, h, rop_write_0s);
  366.             SetTextColor(hdcbit, 0xffffffL);    /* 1 */
  367.             SetBkMode(hdcbit, TRANSPARENT);
  368.             TextOut(hdcbit, 0, 0, &chr, 1);
  369.             GetBitmapBits(hbm, (DWORD)raster * h, bits);
  370.             DeleteDC(hdcbit);
  371.             win_release_dc(fdev, hdcwin);
  372.             DeleteObject(hbm);
  373.             if ( !wxf->invert_y )
  374.                 code = (*copy_mono)(dev, bits, 0,
  375.                         raster, gx_no_bitmap_id,
  376.                         xo, y, w, h,
  377.                         gx_no_color_index, color);
  378.             else
  379.             {    /* Copy scan lines in reverse order. */
  380.                 int i;
  381.                 y += h - 1;
  382.                 for ( i = 0; i < h; i++ )
  383.                     (*copy_mono)(dev, bits + i * raster,
  384.                         0, raster, gx_no_bitmap_id,
  385.                         xo, y - i, w, 1,
  386.                         gx_no_color_index, color);
  387.             }
  388.         }
  389.         gs_free(bits, h, raster, "win_render_char");
  390.     }
  391.     return (code < 0 ? code : 0);
  392. }
  393.  
  394. /* Release an xfont. */
  395. private int
  396. win_release(gx_xfont *xf, const gs_memory_procs *mprocs)
  397. {    if ( wxf->hFont )
  398.     {    DeleteObject(wxf->hFont);
  399.         wxf->hFont = 0;
  400.     }
  401.     if ( mprocs != NULL )
  402.         (*mprocs->free)((char *)xf, 1, sizeof(win_xfont),
  403.                 "win_release");
  404.     return 0;
  405. }
  406.  
  407. /* ------ Font utilities ------ */
  408.  
  409. #undef wdev
  410. #undef wxf
  411.  
  412. /* Get a DC for the font's device. */
  413. private HDC near
  414. win_get_dc(gx_device_win *wdev)
  415. {    return GetDC(wdev->hwndimg);
  416. }
  417.  
  418. /* Release a DC for the font's device. */
  419. private void near
  420. win_release_dc(gx_device_win *wdev, HDC hdc)
  421. {    ReleaseDC(wdev->hwndimg, hdc);
  422. }
  423.  
  424. /* Make an xfont current, possibly remapping it from a logical font. */
  425. private int
  426. win_select_font(HDC hdc, win_xfont *wxf)
  427. {    HFONT hFont = wxf->hFont;
  428.     if ( hFont == NULL )
  429.     {    /* The font was released to free up resources. */
  430.         /* Re-acquire it now. */
  431.         wxf->hFont = CreateFontIndirect(&wxf->lf);
  432.         if ( wxf->hFont == NULL )
  433.             return gs_error_limitcheck;
  434.     }
  435.     SelectObject(hdc, wxf->hFont);
  436.     return 0;
  437. }
  438.